﻿/*
*┌────────────────────────────────────────────────┐
*│　描    述：DealInnerCommService                                                   
*│　作    者： (Jxw)                                             
*│　版    本：1.0     
*│　功能描述：                                          
*│　创建时间：2021/5/19 14:14:32                        
*└────────────────────────────────────────────────┘
*/

using Anchor120V7.InnerComm;
using Anchor120V7.InnerComm.InnerCommModel;
using Business;
using Data;
using Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace Service
{
    public class DealInnerCommService
    {
        /// <summary>
        /// 私有变量定义
        /// 19-9-20
        /// </summary>
        private static DealInnerCommService m_Instance = null;//单实例
        public InnerCommEntrance m_InnerComm = null;//内部通信接口初始化

        private int m_CommonPort = Convert.ToInt32(ConfigurationManager.AppSettings["InnerCommPort"]);//内部通信端口号
        private int m_GpsPort = Convert.ToInt32(ConfigurationManager.AppSettings["GpsPort"]);
        private int m_JsonPort = Convert.ToInt32(ConfigurationManager.AppSettings["JsonPort"]);
        private string m_V7LocalIP = ConfigurationManager.AppSettings["V7LocalIP"];//本机地址
        public static string BroadCastIP = ConfigurationManager.AppSettings["BroadCastIP"];

        //private static HCStateData hCState = new HCStateData();
        //private static Dictionary<string, TaskDTO> hCState=new Dictionary<string, TaskDTO>();
        //private static string ZDCode = null;

        /// <summary>
        /// 构造函数
        /// 19-9-20
        /// </summary>

        public DealInnerCommService()
        {
            m_InnerComm = new InnerCommEntrance();//内部通信接口
            InnerCommEntrance.InnerCommPort = m_CommonPort;//内部通信端口
            InnerCommEntrance.ListerIP = m_V7LocalIP;//本地IP
            InnerCommEntrance.JsonPort = m_JsonPort;         
            InnerCommEntrance.BroadCastIP = BroadCastIP;
        }

        /// <summary>
        /// 单实例
        /// 19-9-20
        /// </summary>
        public static DealInnerCommService Instance//实例
        {
            get
            {
                if (m_Instance == null)
                    m_Instance = new DealInnerCommService();
                return m_Instance;
            }
        }

        public InnerCommEntrance Entrance
        {
            get
            {
                return m_InnerComm;
            }
        }


        public void start()
        {
            //m_InnerComm.GetReceiveInstence().ReceiveGpsChange += new OnReceiveGPSChangeDelegate(ReceiveGpsChange);//车辆GPS广播消息
            //m_InnerComm.GetReceiveInstence().ReceiveWebServiceRefresh += new OnReceiveWebServiceRefreshDelegate(OnReceiveWebServiceRefresh);
            //m_InnerComm.GetReceiveInstence().ReceiveAmbulanceChange += new OnReceiveAmbulanceChangeDelegate(ReceiveAmbulanceChange);
            m_InnerComm.GetReceiveInstence().ReceiveJSON += DealInnerCommService_ReceiveJSON;
            m_InnerComm.GetReceiveInstence().ReceiveSingleAmbChangeState += DealInnerCommService_ReceiveSingleAmbChangeState;
            m_InnerComm.ReceiveJsonStart();
            m_InnerComm.ReceiveInnerCommStart();
            EventUtility.OnShowMsg("内部通信服务启动成功！", false);
        }

     



        #region 内部通讯接收消息
     
        private void DealInnerCommService_ReceiveJSON(JSON_AnchorInfo jInfo)
        {
            try
            {
                switch (jInfo.MsgType)
                {
                    case "ReceivePolice":
                        EventUtility.OnShowMsg("收到绑定消息   转单编码为：" + jInfo.ObjContent, false);
                        LogUtility.Debug("Form1_ReceiveJSON", "收到绑定消息   转单编码为：" + jInfo.ObjContent);
                        TaskDTO task = DapperData.GetZYTaskInfo(jInfo.ObjContent);

                        LogUtility.Debug("ReceivePolice", " 车辆编码为："+task.ambCode);

                        task.zdCode = jInfo.ObjContent;
                        task.ambStateCode = 0;
                        task.isTold = false;
                        TaskDTO taskDTO = DapperData.GetAmbStatueInfo(task.ambCode);
                        string json = JsonHelper.SerializeObject(taskDTO);
                        LogUtility.Debug("DealInnerCommService_ReceiveJSON/ReceivePolice", "获取车辆状态信息" + json);
                        if (taskDTO == null)
                        {
                            bool ret = DapperData.SaveAmbStatueInfo(task);
                            if (ret)
                            {
                                LogUtility.Debug("DealInnerCommService_ReceiveJSON/SaveAmbStatueInfo", "保存车辆状态信息成功" + json);
                            }
                            else {
                                LogUtility.Debug("DealInnerCommService_ReceiveJSON/SaveAmbStatueInfo", "保存车辆状态信息失败" + json);
                            }

                            bool updateRet=ToldDapperData.UpdateCommuPatientTask(task);
                            if (ret)
                            {
                                LogUtility.Debug("DealInnerCommService_ReceiveJSON/UpdateCommuPatientTask", "更新社区信息任务成功");
                            }
                            else
                            {
                                LogUtility.Debug("DealInnerCommService_ReceiveJSON/UpdateCommuPatientTask", "更新社区信息任务失败");
                            }
                        }
                        else
                        {
                            bool ret = DapperData.DelAmbStatueInfo(task.ambCode);
                            if (ret)
                            {
                                LogUtility.Debug("DealInnerCommService_ReceiveJSON/DelAmbStatueInfo", "删除车辆状态信息成功" + json);
                            }
                            else
                            {
                                LogUtility.Debug("DealInnerCommService_ReceiveJSON/DelAmbStatueInfo", "删除车辆状态信息失败" + json);
                            }
                        }

                        break;
                }
            }
            catch (Exception ex)
            {
                LogUtility.Error("Form1_ReceiveJSON", ex.Message);
            }
        }

        /// <summary>
        /// 监控车辆状态改变
        /// </summary>
        /// <param name="info"></param>
        private void DealInnerCommService_ReceiveSingleAmbChangeState(Broadcast_SingleAmbChangeStateInfo info)
        {
            try
            {
               
                LogUtility.Debug("DealInnerCommService_ReceiveSingleAmbChangeState", "收到单个车辆状态改变, 车辆编码: " + info.AmbCode + ", 状态编码: " + info.StateId);
                TaskDTO taskDto = new TaskDTO();
                if (info.StateId == 4||info.StateId==2||info.StateId==5)
                {
                    EventUtility.OnShowMsg("收到单个车辆状态改变,车辆编码:" + info.AmbCode + ",状态编码:" + info.StateId, false);
                    //获取任务信息
                    taskDto = DapperData.GetAmbStatueInfo(info.AmbCode);
                    LogUtility.Error("DealInnerCommService_ReceiveSingleAmbChangeState", "获取车辆状态信息"+JsonHelper.SerializeObject(taskDto));
                    if (taskDto!=null&&taskDto.ambCode.Contains(info.AmbCode))
                    {
                        switch (info.StateId) {
                            case 2:
                                EventUtility.OnShowMsg("推送驶向现场的调度信息 车辆编码为：" + taskDto.ambCode, false);
                                DealInnerCommBusiness.Business.SendDisPatientInfo(taskDto.zdCode, taskDto.ambCode, taskDto.taskCode, info.StateId);
                                taskDto.ambStateCode = 2;
                                DapperData.UpdateAmbStatueInfo(taskDto);
                                break;
                            case 4:
                                EventUtility.OnShowMsg("推送离开现场的调度信息 任务编码为：" + taskDto.taskCode, false);
                                
                                DealInnerCommBusiness.Business.SendLeaveSceneInfo(taskDto.ambCode, taskDto.taskCode, taskDto.zdCode, info.StateId);
                                taskDto.ambStateCode = 4;
                                DapperData.UpdateAmbStatueInfo(taskDto);
                                break;
                            case 5:
                                EventUtility.OnShowMsg("到达医院的调度信息 任务编码为：" + taskDto.taskCode, false);
                                DealInnerCommBusiness.Business.SendArriveHospInfo(taskDto.ambCode, taskDto.taskCode, taskDto.zdCode, info.StateId);
                                DapperData.DelAmbStatueInfo(taskDto.ambCode);
                                break;
                        }
                    }

                    //查询是否有告知信息
                    TToldInfo toldInfo=DapperData.GetToldInfo(info.AmbCode);
                    if (toldInfo != null) {
                        if (info.StateId==5) {
                            EventUtility.OnShowMsg("到达医院的告知调度信息 任务编码为：" + toldInfo.taskCode, false);
                            WDPushBusiness.PushBusiness.pushPatientResuorceData(toldInfo);
                            //PatientResuorce.patientResuorce.GetDisPatientInfo(toldInfo.toldCode);
                        }
                    }
                    if (info.StateId>5)
                    {
                        bool result=DapperData.DelAmbStatueInfo(info.AmbCode);
                    }
                }
                //LogUtility.Debug("MainForm_ReceiveSingleAmbChangeState", "收到单个车辆状态改变,车辆编码:" + info.AmbCode + ",状态编码:" + info.StateId);
            }
            catch (Exception ex)
            {
                LogUtility.Error("DealInnerCommService_ReceiveSingleAmbChangeState", ex.Message);
            }
        }

        /// <summary>
        /// 多个车辆状态改变
        /// </summary>
        /// <param name="ambChangeInfo"></param>
        //void ReceiveAmbulanceChange(BroadCast_AmbulanceChangeInfo ambChangeInfo)
        //{
        //    EventUtility.OnShowMsg("收到多个车辆状态改变", false);
        //    foreach (var item in ambChangeInfo.AmbCode2State)
        //    {
        //        EventUtility.OnShowMsg("车辆编码:" + item.Key + ",状态编码:" + item.Value + ",准备交给单个进行发送", false);
        //        Broadcast_SingleAmbChangeStateInfo info = new Broadcast_SingleAmbChangeStateInfo();
        //        info.AmbCode = item.Key;
        //        info.StateId = item.Value;
        //        ReceiveSingleAmbChangeState(info);
        //        LogUtility.Debug("ReceiveAmbulanceChange", "多个车辆状态改变,车辆编码:" + item.Key.ToString() + ",状态编码:" + item.Value.ToString());
        //    }
        //}






        #endregion
    }
}
